package com.alin.swing.component.base;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * @ClassName J7JPasswordFieldMain
 * @Description: 密码框。JPasswordField 继承自 JTextField，只是显示输入的内容时用特定的字符替换显示（例如 * 或 ●），用法和 JTextField 基本一致。
 * @Author chenlin
 * @Date 2021/6/7 15:47
 * @Version V1.0
 **/
public class J7JPasswordFieldMain {
    public J7JPasswordFieldMain(){
        JFrame jf = new JFrame("测试窗口");
        jf.setSize(300, 300);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        // 创建密码框，指定可见列数为10列
        final JPasswordField passwordField = new JPasswordField(10);
        panel.add(passwordField);

        // 创建一个按钮，点击后获取密码框中输入的密码
        JButton btn = new JButton("提交");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("提交，密码为: " + new String(passwordField.getPassword()));
            }
        });
        panel.add(btn);

        jf.setContentPane(panel);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new J7JPasswordFieldMain();
    }
}
